<?php

class SearchController extends My_Controller_Action {

    public function init() {
        parent::init();
        $this->setContentTitle(_('XXX_SEARCH_PAGE_TITLE'));
    }

    public function indexAction() {
        
    }

    public function eventAction() {
        $keyword = $this->_getParam('q');
        $search = new Application_Model_DAO_Search();
        $select = $search->getSimpleEventSearch($keyword);
        $this->view->list = $this->createPaginator($select);
    }

    public function catalogAction() {
        $keyword = $this->_getParam('q');
        $searchCode = $this->_getParam('id');
        $search = new Application_Model_DAO_Search();
        if (empty ($searchCode)) {
            $select = $search->getSimpleCatalogSearch($keyword);
        } else {
            $daoCatalog = new Application_Model_DAO_CatalogCategory();
            $category = $daoCatalog->getByCode($searchCode);
            $select = $search->getCatalogSearchByCategory($keyword, $category['catalog_category_id']);
        } 
        $this->view->list = $this->createPaginator($select);
    }

    public function offerAction() {
        $keyword = $this->_getParam('q');
        $searchCode = $this->_getParam('id');
        $search = new Application_Model_DAO_Search();
        $mode = $this->_getParam('mode', 'simple');
        if($mode == 'adv') {
            $tid = $this->_getParam('tid');
            $this->view->searchForm = $form = new Application_Form_Offer_SearchGeneric($tid);
            $form->populate($_GET);
            $data = $form->getValues();
            $select = $search->getAdvancedOfferSearch($data);
        } elseif (empty ($searchCode)) {
            $select = $search->getSimpleOfferSearch($keyword);
        } else {
            $daoCategory = new Application_Model_DAO_Category();
            $category = $daoCategory->getByCode($searchCode);
            $select = $search->getOfferSearchByCategory($keyword, $category['category_id']);
        }        
        $this->view->list = $this->createPaginator($select);
    }

    public function postDispatch() {
        $action = $this->_request->getActionName();
        if (!$this->isAjax() && in_array($action, array('offer', 'catalog', 'avent'))) {
            $name = $action . 'Result';
            if(empty($this->view->list)) {
                $this->render('no-result');
            }
            $this->_helper->viewRenderer->postDispatch();
            $html = $this->getResponse()->getBody();
            $this->getResponse()->clearBody();
            $this->view->$name = $html;
            $this->render('index');
        }

        parent::postDispatch();
    }

//    private function processOuterView($name) {
//        $this->setNoViewRenderer();
//        $this->view->$name = $this->renderScript('search/'.$this->getRequest()->getActionName().'.phtml');
//        $this->view->$name = 'asdf';
//        $this->render('index');
//    }
}

